package nl.rainbowsheep.reisorg.rest.service.controller;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import nl.rainbowsheep.reisorg.rest.service.model.Trip;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.TripViewmodel;
import nl.rainbowsheep.reisorg.rest.service.service.TripService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.acls.model.NotFoundException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/trip")
public class TripController {

    @Autowired
    private TripService tripService;

    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    public List<TripViewmodel> getTrips(@RequestParam(value = "predefined", required = false, defaultValue = "true") boolean predefined) throws NotFoundException {
        List<TripViewmodel> tripsviewmodel = null;
        try {
            tripsviewmodel = tripService.getAll(predefined);
        } catch (Exception ex) {
            Logger.getLogger(TripController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tripsviewmodel;
    }

    @RequestMapping(value = "/gettrip", method = RequestMethod.GET)
    public TripViewmodel getTrip(@RequestParam(value = "id", required = true) long id) throws Exception {
        TripViewmodel trip = null;
        try {
            trip = tripService.findById(id);
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
        return trip;
    }

    /**
     * Method to get all trips searched for.
     *
     * @param days
     * @param persons
     * @param request countries
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/searchresult")
    public List<TripViewmodel> getAllTripsBySearch(@RequestParam(value = "days", defaultValue = "0") int days,
            @RequestParam(value = "persons", defaultValue = "2") int persons,
            HttpServletRequest request) throws Exception {
        String[] countryNames = request.getParameterValues("country");
        List<String> countries = Arrays.asList(countryNames);
        List<TripViewmodel> searchedTrips = tripService.getBySearch(days, persons, countries);
        return searchedTrips;
    }

    /**
     * Method saves a TripViewmodel object to store the Trip object with
     * corresponding accommodations
     *
     * @param trip
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Trip saveTripViewmodel(@RequestBody Trip trip) throws Exception {
        try {
            return tripService.save(trip);
        } catch (Exception ex) {
            throw new Exception(ex.toString());
        }
    }
}
